package com.example.preventlosing;

import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class RegisterActivity extends Activity {

	Button RegCommitBtn;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		
		RegCommitBtn = (Button)findViewById(R.id.loginButton);
		RegCommitBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				TCPSever server = new TCPSever();
				server.start();
				Intent intent = new Intent(RegisterActivity.this, SettingActivity.class);
				startActivity(intent);
			}
		});

				
	}
	
	
	public class TCPSever extends Thread {
		
		private InputStream is;
		private OutputStream os;
		private ServerSocket serverSocket = null;
		private ObjectInputStream ois;
		ObjectOutputStream oos;
		Socket socket = null;
		private RegisterActivity ra;
		
		
		public void run() {
			
			try {
			
			serverSocket = new ServerSocket(3007);
			while(true) {
				Socket socket = serverSocket.accept();
				is = socket.getInputStream();
				os = socket.getOutputStream();
				ois = new ObjectInputStream(is);
				oos = new ObjectOutputStream(os);
				String msg = (String)ois.readObject();
				if(msg.equals("gps")) {
					LocationManager lManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
					lManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
					lManager.getProviders(true);
					
					Criteria c = new Criteria();
					c.setAccuracy(Criteria.ACCURACY_FINE);
					c.setPowerRequirement(Criteria.POWER_LOW);
					
					String provider = lManager.getBestProvider(c, true);
					Location loc = lManager.getLastKnownLocation(provider);

					


					
					double lat = loc.getLatitude();
					double lon = loc.getLongitude();
					
					String retMsg = lat +","+lon;
					Log.e("gps",retMsg);
					
					oos.writeObject(retMsg);
					
				}
				else if(msg.equals("sound")) {
					Vibrator vib = null;
					vib = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
					
					SoundPool sp = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
					int sId = sp.load(RegisterActivity.this, R.raw.sound,1);
					sp.play(sId, 1, 1, 0, 0, 1);
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					sp.play(sId, 1, 1, 0, 0, 1);

					for(int i = 0 ; i < 10; i++) {
						vib.vibrate(200);
						try {
							Thread.sleep(300);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
				else if(msg.equals("camera")) {
					Intent i = new Intent(RegisterActivity.this,CameraActivity.class);
					startActivity(i);
					while(ImageAdapter.ImageData==null);
					byte[] imageData = ImageAdapter.ImageData;
					
					oos.writeObject(imageData);
					
				}
				socket.close();
				
			}
			
			}catch(Exception e){e.printStackTrace();}
			
			
		}

	}
}
